﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using MahApps.Metro.Controls;
using SpectrumAnalyser.Common.Helpers;
using SpectrumAnalyser.Common.WpfValidation;
using SpectrumAnalyser.Shell.ViewModels;

namespace SpectrumAnalyser.Shell.Controls
{
    /// <summary>
    /// Interaction logic for CreateItemDialog.xaml
    /// </summary>
    public partial class CreateItemDialog : MetroWindow
    {
        public CreateItemDialog()
        {
            InitializeComponent();
            Item = new CreateItemViewModel();
            this.DataContext = Item;
        }

        public event ValidateItemName OnValidateItemName;
        public delegate string ValidateItemName(string name);

        public CreateItemViewModel Item { get; set; }

        private void Ok(object sender, RoutedEventArgs e)
        {
            if (!ReferenceEquals(OnValidateItemName, null))
            {
                Item.ValidationRules.Add(StaticReflection.GetMemberName<CreateItemViewModel>(x => x.ItemName), () =>
                {
                    return OnValidateItemName(Item.ItemName);
                });
            }

            if (ErrorProvider.Validate())
            {
                this.DialogResult = true;
                this.Close();
            }
        }

        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
